package org.grow.jssp.service;

import org.grow.jssp.entity.JsspFile;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
/**lambda表达式又叫箭头函数针对list处理*/
public class FileLambdaOps implements FileOperator{
    @Override
    public List<JsspFile> ops(String filename) {
        Path path=Paths.get(filename);
        try {
            Stream<Path> allFiles=Files.list(path);
            System.out.println("====================");
//            System.out.println(allFiles.collect(Collectors.toList()));
//            lambda表达式过滤
          /*  Stream<Path> pathStream=allFiles.filter((e)->{
                return Files.isRegularFile(e);
            });
            Stream<JsspFile> jsspFileStream=pathStream.map((e)->{
                JsspFile jsspFile=new JsspFile();
                try {
                    jsspFile.setSize((int)Files.size(e));
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                jsspFile.setTitle(e.getFileName().toString());
                return jsspFile;
            });
            System.out.println(jsspFileStream.collect(Collectors.toList()));*/
            List<JsspFile> collect=allFiles.filter(e->{
                return  Files.isRegularFile(e);
            }).map((e)->{
                JsspFile jsspFile=new JsspFile();
                try {
                    jsspFile.setSize((int)Files.size(e));
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                jsspFile.setTitle(e.getFileName().toString());
                return jsspFile;
            }).collect(Collectors.toList());
            System.out.println(collect);
            return collect;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
